<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        const orders = [
            {
                id: 1,
                name: 'John Smith',
                address: '123 Main St',
                status: 'created',
                log: [
                    {
                        status: 'created',
                        time: '2023-02-16 12:30:00'
                    }
                ]
            },
            {
                id: 2,
                name: 'Jane Doe',
                address: '456 Second St',
                status: 'shipped',
                log: [
                    {
                        status: 'created',
                        time: '2023-02-16 15:00:00'
                    },
                    {
                        status: 'packed',
                        time: '2023-02-16 12:30:00'
                    }
                ]
            },
            {
                id: 3,
                name: 'Bob Johnson',
                address: '789 Third St',
                status: 'shipped',
                log: [
                    {
                        status: 'created',
                        time: '2023-02-15 10:00:00'
                    },
                    {
                        status: 'packed',
                        time: '2023-02-15 12:00:00'
                    },
                    {
                        status: 'shipped',
                        time: '2023-02-16 9:00:00'
                    }
                ]
            }
        ];


        function getOrdersByStatus(status) {
            const getStatus = orders.filter(function (item) {
                return item.status === status
            })
          //  console.log(getStatus);
          const newArr =  getStatus.map(function (item) {
                return {
                id: item.id,
                name: item.name,
                address:item. address,
                status:item. status,
                log:item.log.filter(item => item.status === status)
                }
            }).sort((a,b) => a.id - b.id)
            return newArr

        }

        console.log(getOrdersByStatus('created'));
        console.log(getOrdersByStatus('packed'));
        console.log(getOrdersByStatus('shipped'));




        function getOrderStatusHistory(id) {
            const arrn = orders.filter(item => item.id === id)
            const [{ id: Ids, name, address, status, log }] = arrn
            log.sort(function (a, b) {
                if (a.time < b.time) {
                    return -1
                }
            })
            const str = log.map((e) => `[${e.time}] ${e.status}`).join('\n')

            return str
        }
        console.log(getOrderStatusHistory(1))
        console.log(getOrderStatusHistory(2))
        console.log(getOrderStatusHistory(3))

    </script>
</body>

</html>